<?php
/**
 * Created by : PhpStorm
 * @author: SongKeJing qq:597481334 mobile:159-5107-8050
 * @date: 2021/7/13 14:07
 */

namespace BetvSys;

use ClassLibrary\ClHttp;

/**
 * 基础
 * Class BetvSysBase
 * @package BetvSys
 * @author SongKeJing qq:597481334 mobile:159-5107-8050
 * @date 2021/7/13 14:12
 */
class BetvSysBase {

    /**
     * 实例对象数组
     * @var static
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2020/12/9 15:54
     */
    protected static $instances = [];

    /**
     * 实例化
     * @return static
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2020/12/9 16:10
     */
    public static function instance() {
        //依据当前类解决冲突问题
        $static_class_name = static::class;
        if (!isset(self::$instances[$static_class_name])) {
            self::$instances[$static_class_name] = new static();
        }
        return self::$instances[$static_class_name];
    }

    /**
     * 请求地址
     * @var string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:13
     */
    protected $host = '';

    /**
     * 设置host
     * @param $host
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:14
     */
    public function setHost($host) {
        $this->host = $host;
        return $this;
    }

    /**
     * 端口
     * @var string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:15
     */
    protected $port = '';

    /**
     * 设置端口
     * @param int $port
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:15
     */
    public function setPort(int $port) {
        $this->port = $port;
        return $this;
    }

    /**
     * 请求地址
     * @var string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:17
     */
    protected $address = '';

    /**
     * 设置请求地址
     * @param string $address
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:17
     */
    public function setAddress(string $address) {
        $this->address = $address;
        return $this;
    }

    /**
     * 参数
     * @var array
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:17
     */
    protected $params = [];

    /**
     * 设置参数
     * @param array $params
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:20
     */
    public function setParams(array $params) {
        $this->params = array_merge($this->params, $params);
        return $this;
    }

    /**
     * 用户名
     * @var string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:21
     */
    protected $username = '';

    /**
     * 设置用户名
     * @param string $username
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:22
     */
    public function setUsername(string $username) {
        $this->username = $username;
        return $this;
    }

    /**
     * 密码
     * @var string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:22
     */
    protected $password = '';

    /**
     * 设置密码
     * @param string $password
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:23
     */
    public function setPassword(string $password) {
        $this->password = $password;
        return $this;
    }

    /**
     * debug
     * @var bool
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 16:40
     */
    protected $is_debug = false;

    /**
     * 设置debug
     * @param bool $is_debug
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 16:40
     */
    public function setIsDebug($is_debug = false) {
        $this->is_debug = $is_debug;
        return $this;
    }

    /**
     * 缓存时间
     * @var int
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 16:41
     */
    protected $duration = 0;

    /**
     * 设置缓存时间
     * @param int $duration
     * @return $this
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 16:41
     */
    public function setDuration($duration = 0) {
        $this->duration = $duration;
        return $this;
    }

    /**
     * 调用请求
     * @return bool|mixed|string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2021/7/13 14:30
     */
    public function call() {
        //合并参数
        $this->params['username'] = $this->username;
        $this->params['pass']     = $this->password;
        $url                      = sprintf('http://%s:%s%s?%s', $this->host, $this->port, $this->address, http_build_query($this->params));
        $result                   = ClHttp::request($url, [], ClHttp::REQUEST_RESULT_TYPE_JSON, $this->duration, $this->is_debug);
        //重置数据
        $this->setAddress('')
            ->setParams([])
            ->setIsDebug(false)
            ->setDuration(0);
        return $result;
    }

}